package com.learning.java.optimize.design.pattern.strategy;

import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: FeeCalculationService
 * Description: 收银服务设计
 * Date: 2020/3/22 17:11 【需求编号】
 *
 * @author Sam Sho
 * @version V1.0.0
 */
@Service
public class FeeCalculationServiceDesign2 {

    private Map<String, DiscountStrategy> discountStrategyMap = new HashMap<>();

    /**
     * 集合注入
     *
     * @param discountStrategyList
     */
    public FeeCalculationServiceDesign2(List<DiscountStrategy> discountStrategyList) {
        for (DiscountStrategy discountStrategy : discountStrategyList) {
            discountStrategyMap.put(discountStrategy.type(), discountStrategy);
        }
    }

    /**
     * 收银方法
     *
     * @param type 会员类型
     * @param fee  费用
     * @return
     */
    public double calculation(String type, double fee) {
        DiscountStrategy discountStrategy = discountStrategyMap.get(type);
        return discountStrategy.discount(fee);
    }
}
